<template>
  <t-dialog :header="header"  :confirmBtn="null" :visible.sync="visible" :onClose="closeDialog"     :closeOnOverlayClick="false"
            width="80%"
            top="5%">
    <div style="height: 70vh">

    <div v-if="editOpen" class="btn-group-inline">
      <t-button @click="dialog.open_add_edit_award=true"  variant="outline" theme="primary">
        <t-icon name="add" slot="icon"/>
        添加奖项
      </t-button>
    </div>
    <t-table row-key="id" :columns="columns"
             :data="data" :selected-row-keys="selectedRowKeys" @select-change="(val)=>{selectedRowKeys=val}">
      <template #awardsLevel="{row}">
        {{row.awardsLevel | filterByDict([{label:'一等奖',value:'1'},{label:'二等奖',value:'2'},{label:'三等奖',value:'3'}])}}
      </template>
      <template #awardsAmount="{row}">
        {{row.awardsAmount}}万元
      </template>
      <template #[`innovations.applicationField`]="{row}">
        {{ row.innovations.applicationField | filterByDict(dict.type.application_field) }}
      </template>
      <template #[`innovations.innovationCategory`]="{row}">
        {{ row.innovations.innovationCategory | filterByDict(dict.type.innovation_category) }}
      </template>
      <template #op="{row}">
        <t-button @click="dialog.header=row.innovations.name;dialog.objId=row.innovations.id;dialog.editOpen=false;dialog.open_detail=true"  size="small" variant="text" class="t-button-link">
          <t-icon name="info-circle" slot="icon"/>
          推荐详情
        </t-button>
        <t-button v-if="editOpen"  @click="handleDelete(row)" size="small" variant="text"
                  class="t-button-link">
          <t-icon name="delete" slot="icon" />
          删除
        </t-button>
      </template>
    </t-table>
    </div>
    <add-edit-innovation-award @reload="getList" :header="dialog.header" :year="year" :objId="objId" :visible.sync="dialog.open_add_edit_award" v-if="dialog.open_add_edit_award"></add-edit-innovation-award>
    <recommend-detail-dialog :editOpen="dialog.editOpen" :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_detail" v-if="dialog.open_detail"></recommend-detail-dialog>
  </t-dialog>
</template>

<script>
import AddEditInnovationAward from "./add-edit-innovation-award.vue";
import RecommendDetailDialog from "../../recommend/components/recommend-detail-dialog.vue";

export default {
  name: "innovation-award",
  dicts:['application_field','innovation_category'],
  components:{
    AddEditInnovationAward,
    RecommendDetailDialog
  },
  props: {
    header: {
      type: String,
      default: ''
    },
    objId:{
      type: Number,
      default: 0
    },
    year:{
      type: String,
      default: ''
    },
    editOpen:{
      type: Boolean,
      default: true
    }
  },
  data(){
    return {
      visible: true,
      dialog:{
        header:'',
        objId:0,
        open_add_edit_award: false,
        editOpen: false,
        open_detail: false,
      },
      selectedRowKeys:[],
      data:[],
      columns:[
        {
          colKey: "innovations.name",
          title: "成果名称",
          align: "center",
          width: 220
        },
        {
          colKey: "awardsLevel",
          title: "获奖等级",
          align: "center",
          ellipsis: true
        },
        {
          colKey: "awardsAmount",
          title: "获奖金额",
          align: "center",
          ellipsis: true
        },
        {
          colKey: "innovations.score",
          title: "评审平均分",
          align: "center",
          ellipsis: true
        },
        {
          colKey: "innovations.applicationProject",
          title: "应用项目",
          align: "center",
          ellipsis: true
        },
        {
          colKey: "innovations.applicationField",
          title: "应用领域",
          align: "center",
          ellipsis: true
        },
        {
          colKey: "innovations.innovationCategory",
          title: "创新类别",
          align: "center",
          ellipsis: true
        },
        {
          colKey: "innovations.leaderName",
          title: "负责人",
          align: "center",
          ellipsis: true
        },
        {
          colKey: "innovations.deptName",
          title: "工作单位",
          align: "center",
          ellipsis: true
        },
        {
          colKey: "createTime",
          title: "创建时间",
          align: "center",
          width: 200
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 130
        },
      ]
    }
  },
  created() {
    this.getList();
  },
  methods:{
    getList(){
      this.$api.wcx.apply.innovationAwardsList({annualAwardsId:this.objId}).then(
        (res) => {
          this.data = res.data;
        }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    handleDelete(row){
      const confirmDia = this.$dialog.confirm({
        header:"系统提示",
        body:`是否确认删除成果为"${row.innovations.name}"的数据项？`,
        theme: "warning",
        onConfirm: ({ e }) => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          })
          this.$api.wcx.apply.innovationAwardsDel(row.id).then(
            (res) => {
              this.getList();
              this.$message.success("删除成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }).catch((e) => {
            confirmDia.update({
              confirmBtn: {
                loading: false,
              },
            })
            this.$message.error(e.toString());
          });
        },
      })
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
  }
}
</script>

<style scoped>

</style>
